/*
 * Copyright (C) 2012 nutmotion <simonotmanuel@gmail.com>
 * Originally released under the tradeUplan project (wiki is at Expression project.wiki is undefined on line 5, column 85 in Templates/Licenses/license-gpl30.txt.)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.nutmotion.model.jpa;

import com.nutmotion.utils.StringBuilderAppendHelper;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author nutmotion <simonotmanuel@gmail.com>
 */
@Entity
@Table(name = "trade")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Trade.findAll", query = "SELECT t FROM Trade t"),
    @NamedQuery(name = "Trade.findById", query = "SELECT t FROM Trade t WHERE t.id = :id"),
    @NamedQuery(name = "Trade.findByName", query = "SELECT t FROM Trade t WHERE t.name = :name")})
public class Trade implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User user;
    @JoinColumn(name = "timeframe_id", referencedColumnName = "id")
    @ManyToOne
    private Timeframe timeframe;
    @JoinColumn(name = "stock_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Stock stock;
    @JoinColumn(name = "direction_id", referencedColumnName = "id")
    @ManyToOne
    private Direction direction;
    @JoinTable(name = "trade_analysis",
    joinColumns =
    @JoinColumn(name = "trade_id"),
    inverseJoinColumns =
    @JoinColumn(name = "analysis_id"))
    @ManyToMany
    private Collection<Analysis> analyses;
    @JoinTable(name = "trade_signal",
    joinColumns =
    @JoinColumn(name = "trade_id"),
    inverseJoinColumns =
    @JoinColumn(name = "signal_id"))
    @ManyToMany
    private Collection<Signal> signals;
    @JoinTable(name = "trade_tag",
    joinColumns =
    @JoinColumn(name = "trade_id"),
    inverseJoinColumns =
    @JoinColumn(name = "tag_id"))
    @ManyToMany
    private Collection<Tag> tags;

    @Transient
    private String concatSignals;
            
    @Transient
    private String concatTags;
    
    public Trade() {
    }

    public Trade(Integer id) {
        this.id = id;
    }

    /**
     * Builds an empty Trade object
     * @return 
     */
    public static Trade build() {
        Trade newTrade = new Trade();
        User newUser = new User();
        Timeframe newTimeframe = new Timeframe();
        Stock newStock = new Stock();
        Direction newDirection = new Direction();
        Collection<Signal> newSignals = new ArrayList<Signal>();
        newSignals.add(new Signal());
        Collection<Tag> newTags = new ArrayList<Tag>();
        newTags.add(new Tag());

        newTrade.setUser(newUser);
        newTrade.setTimeframe(newTimeframe);
        newTrade.setStock(newStock);
        newTrade.setDirection(newDirection);
        newTrade.setSignals(newSignals);
        newTrade.setTags(newTags);

        return newTrade;
    }
    
    public String getConcatAnalyses() {
        StringBuilderAppendHelper sbahAnalyses = new StringBuilderAppendHelper("", ",");

        for (Analysis analysis : this.analyses) {
            sbahAnalyses.append(analysis.getName());
        }

        return sbahAnalyses.toString();
    }

    

    public String getConcatSignals() {
        StringBuilderAppendHelper sbahSignals = new StringBuilderAppendHelper("", ",");

        for (Signal signal : this.signals) {
            sbahSignals.append(signal.getName());
        }

        return sbahSignals.toString();
    }

    public String getConcatTags() {
        StringBuilderAppendHelper sbahTags = new StringBuilderAppendHelper("", ",");

        for (Tag tag : this.tags) {
            sbahTags.append(tag.getTag());
        }

        return sbahTags.toString();
    }

    /**
     * A setter wrapper for the analyses collection
     * @return 
     */
    public void setAnalysis(Analysis analysis) {
        if (this.analyses == null) {
            this.analyses = new ArrayList<Analysis>();
        }
        this.analyses.add(analysis);
    }

    /**
     * Dummy getter for JSF...
     */
    public Analysis getAnalysis() {
        return null;
    }

    /**
     * A setter wrapper for the signals collection
     * @return 
     */
    public void setSignal(Signal signal) {
        if (this.signals == null) {
            this.signals = new ArrayList<Signal>();
        }
        this.signals.add(signal);
    }

    /**
     * Dummy getter for JSF...
     */
    public Signal getSignal() {
        return null;
    }

    /**
     * A setter wrapper for the signals collection
     * @return 
     */
    public void setTag(Tag tag) {
        if (this.tags == null) {
            this.tags = new ArrayList<Tag>();
        }
        this.tags.add(tag);
    }

    /**
     * Dummy getter for JSF...
     */
    public Tag getTag() {
        return null;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Timeframe getTimeframe() {
        return timeframe;
    }

    public void setTimeframe(Timeframe timeframe) {
        this.timeframe = timeframe;
    }

    public Stock getStock() {
        return stock;
    }

    public void setStock(Stock stock) {
        this.stock = stock;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Trade)) {
            return false;
        }
        Trade other = (Trade) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.nutmotion.jpa.Trade[ id=" + id + " ]";
    }

    /**
     * @return the signals
     */
    public Collection<Signal> getSignals() {
        return signals;
    }

    /**
     * @param signals the signals to set
     */
    public void setSignals(Collection<Signal> signals) {
        this.signals = signals;
    }

    /**
     * @return the tags
     */
    public Collection<Tag> getTags() {
        return tags;
    }

    /**
     * @param tags the tags to set
     */
    public void setTags(Collection<Tag> tags) {
        this.tags = tags;
    }
}
